local skel = fk.CreateSkill {
  name = "rmt__jimou",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["rmt__jimou"] = "冀谋",
  [":rmt__jimou"] = "转换技，游戏开始时你自选阴阳状态。当你阳：弃置牌；阴：受到伤害后，你可以将弃置牌或一张手牌交给当前回合角色；"..
  "其本回合造成伤害后，受伤角色可以摸一张牌，令其因此获得的牌于本回合视为无次数限制的【杀】。",

  [":rmt__jimou_yang"] = "转换技，游戏开始时你自选阴阳状态。当你<font color=\"#E0DB2F\">阳：弃置牌</font>；阴：受到伤害后，你可以将弃置牌或一张手牌交给当前回合角色；"..
  "其本回合造成伤害后，受伤角色可以摸一张牌，令其因此获得的牌于本回合视为无次数限制的【杀】。",
  [":rmt__jimou_yin"] = "转换技，游戏开始时你自选阴阳状态。当你阳：弃置牌；<font color=\"#E0DB2F\">阴：受到伤害后</font>，你可以将弃置牌或一张手牌交给当前回合角色；"..
  "其本回合造成伤害后，受伤角色可以摸一张牌，令其因此获得的牌于本回合视为无次数限制的【杀】。",

  ["#rmt__jimou1-invoke"] = "冀谋：你可以选择一张手牌交给当前回合角色，或单独选择当前回合角色，交给其所有弃置牌",
  ["#rmt__jimou2-invoke"] = "冀谋：你可以选择一张手牌交给当前回合角色",
  ["@@rmt__jimou1-inhand"] = "冀谋",
  ["rmt__jimou-turn"] = "冀谋",
  ["@@rmt__jimou3-turn"] = "冀谋 无次数限制",
  ["#rmt__jimou-draw"] = "冀谋：是否摸一张牌，令%dest因“冀谋”获得的牌本回合均视为无次数限制的【杀】？",

  ["#rmt__jimou1"] = "切换 冀谋 至阳状态",
  ["#rmt__jimou2"] = "切换 冀谋 至阴状态",
  ["#rmt__jimou-choice"] = "冀谋：请自选本技能阴阳状态",

  ["$rmt__jimou1"] = " ",
  ["$rmt__jimou2"] = " ",
}

local U = require "packages.utility.utility"

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:getSwitchSkillState(skel.name) == fk.SwitchYang
    and player.room.current and not player.room.current.dead then
      local cards = {}
      for _, move in ipairs(data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      cards = table.filter(cards, function(id)
        return table.contains(player.room.discard_pile, id)
      end)
      cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "rmt__jimou_active",
      prompt = "#rmt__jimou1-invoke",
      extra_data = {must_give = false},
    })
    if dat then
      local cardss = #dat.cards > 0 and dat.cards or event:getCostData(self).cards
      event:setCostData(self, {tos = room.current, cards = cardss})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    U.SetSwitchSkillState(player, skel.name, player:getSwitchSkillState(skel.name, false), {"rmt__", "rmt1__"})
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos
    room:obtainCard(to, cards, true, fk.ReasonGive, player, skel.name, "@@rmt__jimou1-inhand")
    if room.current and not room.current.dead then
      room:setPlayerMark(room.current, "rmt__jimou-turn", 1)
    end
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:getSwitchSkillState(skel.name) == fk.SwitchYin
    and target == player and not player:isKongcheng() and player.room.current and not player.room.current.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "rmt__jimou_active",
      prompt = "#rmt__jimou2-invoke",
      extra_data = {must_give = true},
    })
    if dat and #dat.cards > 0 then
      local cardss = dat.cards
      event:setCostData(self, {tos = room.current, cards = cardss})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    U.SetSwitchSkillState(player, skel.name, player:getSwitchSkillState(skel.name, false), {"rmt__", "rmt1__"})
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos
    room:obtainCard(to, cards, true, fk.ReasonGive, player, skel.name, "@@rmt__jimou1-inhand")
    if room.current and not room.current.dead then
      room:setPlayerMark(room.current, "rmt__jimou-turn", 1)
    end
  end,
})

skel:addEffect(fk.Damage, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and target:getMark("rmt__jimou-turn") > 0 and not target.dead and data.to and not data.to.dead
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(data.to, { skill_name = skel.name, prompt = "#rmt__jimou-draw::"..player.id,})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.to:drawCards(1, skel.name, "top", "@@rmt__jimou1-inhand")
    if not player.dead then
      room:setPlayerMark(room.current, "@@rmt__jimou3-turn", 1)
    end
  end,
})

skel:addEffect("filter", {
  mute = true,
  card_filter = function(self, to_select, player)
    return to_select:getMark("@@rmt__jimou1-inhand") > 0 and table.contains(player:getCardIds("h"), to_select.id) and player:getMark("@@rmt__jimou3-turn") > 0
  end,
  view_as = function(self, player, card)
    return Fk:cloneCard("slash", card.suit, card.number)
  end,
})

skel:addEffect("targetmod", {
  mute = true,
  bypass_times = function(self, player, skill, scope, card, to)
    return card and skill.trueName == "slash_skill" and card:getMark("@@rmt__jimou1-inhand") > 0 and player:getMark("@@rmt__jimou3-turn") > 0
  end,
})

skel:addEffect(fk.GameStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = { "#rmt__jimou1", "#rmt__jimou2" },
      skill_name = skel.name,
      prompt = "#rmt__jimou-choice",
    })
    local state = choice:endsWith("1") and fk.SwitchYang or fk.SwitchYin
    U.SetSwitchSkillState(player, skel.name, state, {"rmt__", "rmt1__"})
  end,
})

return skel
